package com.gxa.service.zijinservice.impl;

import com.gxa.common.R;
import com.gxa.mapper.zijinmapper.ShouKuanMapper;
import com.gxa.pojo.*;
import com.gxa.service.zijinservice.ShouKuanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author zhaohaoc
 * @version 1.0
 * @date 2022/4/2 11:26
 */
@Service
public class ShouKuanServiceImpl implements ShouKuanService {

    @Autowired
    public ShouKuanMapper shouKuanMapper;

    @Override
    public R selectAllKeHu() {
        R r = new R();
        List<KeHu> keHuList = shouKuanMapper.selectAllKeHu();
        if (keHuList != null){
            r.setCode(200);
            r.setMsg("success");
            r.setData(keHuList);
        }
        return r;
    }

    @Override
    public R selectAllZhangHu() {
        R r = new R();
        List<ZhangHu> zhangHuList = shouKuanMapper.selectAllZhangHu();
        if (zhangHuList != null){
            r.setCode(200);
            r.setMsg("success");
            r.setData(zhangHuList);
        }
        return r;
    }

    @Override
    public R selectJieSuanWay() {
        R r = new R();
        List<JieSuanWay> jieSuanWayList = shouKuanMapper.selectJieSuanWay();
        if (jieSuanWayList != null){
            r.setCode(200);
            r.setMsg("success");
            r.setData(jieSuanWayList);
        }
        return r;
    }

    @Override
    public R selectShouKuanDan(String startTime,String endTime,String shouKuanDanBianHao,String keHuMingCheng,
                               String shouKuanDanBeiZhu) {
        R r = new R();
        List<ShouKuanDan> shouKuanDanList = shouKuanMapper.selectShouKuanDan(startTime,endTime,
                shouKuanDanBianHao, keHuMingCheng, shouKuanDanBeiZhu);
        if (shouKuanDanList != null){
            r.setCode(200);
            r.setMsg("success");
            r.setData(shouKuanDanList);
        }
        return r;
    }

    @Override
    public R selectXiaoHuoDan(String startTime,
                              String endTime,
                              String keHuMingCheng,
                              String xiaoHuoDanBianHao) {
        R r = new R();
        KeHu keHu = shouKuanMapper.selectKeHuIdByKeHuMingCheng(keHuMingCheng);
        long kehuid = keHu.getId();
        String keHuId = String.valueOf(kehuid);
        List<XiaoHuoDan> xiaoHuoDanList = shouKuanMapper.selectXiaoHuoDan(startTime, endTime, keHuId, xiaoHuoDanBianHao);

        if (xiaoHuoDanList != null){
            r.setCode(200);
            r.setMsg("success");
            r.setData(xiaoHuoDanList);
        }
        return r;
    }

    @Override
    public R delShouKuanDanByBianHao(String shouKuanDanBianHao) {
        R r = new R();
        long shouKuanDanId = shouKuanMapper.selectShouKuanDanByShouKuanBianHao(shouKuanDanBianHao);
        int result =shouKuanMapper.delShouKuanDanByBianHao(shouKuanDanBianHao);
        int result1 = shouKuanMapper.delShouKuanDanInFo(shouKuanDanId);
        int result2 = shouKuanMapper.delShouKuanXiaoHuo(shouKuanDanId);
        if(result > 0){
            r.setCode(200);
            r.setMsg("success");
        }
        return r;
    }

    @Override
    public R insertShuoKuanDan(ShouKuanDan shouKuanDan){
        R r = new R();
        try {
            Date date = shouKuanDan.getShouKuanDanRiQi();
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
            String shouKuanDanRiQi = sd.format(date);

            String shouKuanDanBianHao = shouKuanDan.getShouKuanDanBianHao();

            long keHuId = shouKuanDan.getKeHuId();

            String shouKuanDanBeiZhu = shouKuanDan.getShouKuanDanBeiZhu();

            String shouKuanDanZongJinE = String.valueOf(shouKuanDan.getShouKuanZongJinE());

            int result = shouKuanMapper.addShouKuanDan(shouKuanDanRiQi, shouKuanDanBianHao, keHuId,shouKuanDanBeiZhu,
                    shouKuanDanZongJinE);

            long shouKuanDanId = shouKuanMapper.selectShouKuanDanByShouKuanBianHao(shouKuanDanBianHao);

            if (shouKuanDan.getShouKuanDanZhongJianBiaoList().size() > 0){
                for (ShouKuanDanZhongJianBiao sdzjb:shouKuanDan.getShouKuanDanZhongJianBiaoList()) {
                    BigDecimal shouKuanJinE = sdzjb.getShouKuanJinE();
                    long jieSuanWayId = sdzjb.getJieSuanWayId();
                    long zhangHuId = sdzjb.getZhangHuId();
                    shouKuanMapper.addShouKuanDanInFo(zhangHuId,shouKuanJinE,shouKuanDanId,jieSuanWayId);
                }
            }
            if (shouKuanDan.getShouKuanDanXiaoHuoDanList().size() > 0){
                for (ShouKuanDanXiaoHuoDan skdxhd:shouKuanDan.getShouKuanDanXiaoHuoDanList()) {
                    long xiaoHuoDanId = skdxhd.getXiaoHuoDanId();
                    shouKuanMapper.addShouKuanXiaoHuoInFo(shouKuanDanId,xiaoHuoDanId);
                }
            }
            if(result > 0){
                r.setCode(200);
                r.setMsg("success");
            }
        }catch (Exception e){
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员！");
            e.printStackTrace();
        }
        return r;
    }

    @Override
    public R updateShuoKuanDan(ShouKuanDan shouKuanDan) {
        R r = new R();
        try {
            long shouKuanDanId = shouKuanDan.getId();
            String shouKuanDanBeiZhu = shouKuanDan.getShouKuanDanBeiZhu();

            String shouKuanDanZongJinE = String.valueOf(shouKuanDan.getShouKuanZongJinE());

            int result = shouKuanMapper.updateShouKuanDan(shouKuanDanId,shouKuanDanBeiZhu,shouKuanDanZongJinE);

            if (shouKuanDan.getShouKuanDanZhongJianBiaoList().size() > 0){
                for (ShouKuanDanZhongJianBiao sdzjb:shouKuanDan.getShouKuanDanZhongJianBiaoList()) {
                    BigDecimal shouKuanJinE = sdzjb.getShouKuanJinE();
                    long jieSuanWayId = sdzjb.getJieSuanWayId();
                    long zhangHuId = sdzjb.getZhangHuId();
                    shouKuanMapper.updateShouKuanDanInFo(zhangHuId,shouKuanJinE,shouKuanDanId,jieSuanWayId);
                }
            }
            if (shouKuanDan.getShouKuanDanXiaoHuoDanList().size() > 0){
                for (ShouKuanDanXiaoHuoDan skdxhd:shouKuanDan.getShouKuanDanXiaoHuoDanList()) {
                    long xiaoHuoDanId = skdxhd.getXiaoHuoDanId();
                    shouKuanMapper.updateShouKuanXiaoHuoInFo(shouKuanDanId,xiaoHuoDanId);
                }
            }
            if(result > 0){
                r.setCode(200);
                r.setMsg("success");
            }
        }catch (Exception e){
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员！");
            e.printStackTrace();
        }
        return r;
    }

}
